//
//  CCNavigationController.m
//  ccZone
//
//  Created by 陈小娟 on 2017/12/13.
//  Copyright © 2017年 CC. All rights reserved.
//

#import "CCNavigationController.h"

@interface CCNavigationController ()

@end

@implementation CCNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.hidden = YES;// 隐藏navigationbar
}

-(void)pushViewController:(UIViewController *)viewController
                 animated:(BOOL)animated
{
    if ([viewController isKindOfClass:
         NSClassFromString(@"CCHomeController")]||
        [viewController isKindOfClass:
         NSClassFromString(@"CCMineController")]) {
        viewController.hidesBottomBarWhenPushed = NO;
        
    }else
    {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [self.weakArr addPointer:(void *)(viewController)];
    [super pushViewController:viewController animated:animated];
}

-(void)dismissViewControllersInStack {
    if (![self isKindOfClass:NSClassFromString(@"CCViewController")]) {
        //对于不是继承自 CCViewController，不做这一步的处理，
        //会导致返回的时候，navigationBar会显示
        self.navigationController.navigationBar.hidden = NO;
    }
    for (NSUInteger i=self.weakArr.count -1; i>0; i++) {
        UIViewController *obj = [self.weakArr pointerAtIndex:i];
        if ([obj isMemberOfClass:[CCNavigationController class]]) {
            CCNavigationController *nav = (CCNavigationController *)obj;
            [nav dismissViewControllersInStack];
        }else{
            [self popViewControllerAnimated:NO];
        }
    }
    if (_isFirstPageNav) {
        [self resetPointerArray];
    }else{
        self.weakArr = nil;
        [self dismissViewControllerAnimated:NO completion:nil];
    }
}

-(void)resetPointerArray{
    //    self.weakArr
    self.weakArr = [NSPointerArray weakObjectsPointerArray];
    [self.weakArr addPointer:(void *)(self.viewControllers[0])];
}

-(NSPointerArray *)weakArr {
    if (!_weakArr) {
        _weakArr = [NSPointerArray weakObjectsPointerArray];
    }
    return _weakArr;
}

#pragma mark -- 处理旋转问题
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    UIViewController* c = self.topViewController;
    if(c){
        return [c supportedInterfaceOrientations];
    }
    return [super supportedInterfaceOrientations];
}
-(void)dealloc
{
    NSLog(@"TYNavigationController dealloc");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
